1883E - Look Back - CodeForces Solution


bitmasks greedy

Please click on ads to support us..

C++ Code:

// author: rshohruh
// create time: Oct 22 2023, 17:14


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const int MOD = 1e9+7;
const int inf = 1e9;
#define endl "\n"

int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

#define with_testcases
void t_main(){
    int n;
    cin >> n;
    vector<int> a(n);
    for(int &x: a) cin >> x;
    ll ans = 0, cur = 0;
    for(int i = 1; i < n; ++i){
        int x = a[i], y = a[i-1];
        ll res = 0;
        if(x < y) {
            while(x < y){
                x *= 2;
                res ++;
            }
        } else{
            x = a[i], y = a[i-1];
            while(y*2 <= x){
                y *= 2;
                res --;
            }
        }
        cur = max(cur+res, 0LL);
        ans += cur;
    }
    cout << ans;
}

signed main(){
    signed t = 1;
    cin.tie(nullptr)->sync_with_stdio(false);
    #ifdef with_testcases
        cin >> t;
    #endif
    while(t--){
        t_main();
        cout << '\n';
    }
    return 0;
}


/*
██████╗ ███████╗██╗  ██╗ ██████╗ ██╗  ██╗██████╗ ██╗   ██╗██╗  ██╗
██╔══██╗██╔════╝██║  ██║██╔═══██╗██║  ██║██╔══██╗██║   ██║██║  ██║
██████╔╝███████╗███████║██║   ██║███████║██████╔╝██║   ██║███████║
██╔══██╗╚════██║██╔══██║██║   ██║██╔══██║██╔══██╗██║   ██║██╔══██║
██║  ██║███████║██║  ██║╚██████╔╝██║  ██║██║  ██║╚██████╔╝██║  ██║
╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝
*/


Comments

Submit
0 Comments
More Questions

1508B - Almost Sorted
1690C - Restoring the Duration of Tasks
1055A - Metro
1036D - Vasya and Arrays
1139C - Edgy Trees
37A - Towers
353A - Domino
409H - A + B Strikes Back
1262A - Math Problem
158C - Cd and pwd commands
194A - Exams
1673B - A Perfectly Balanced String
1104B - Game with string
1169B - Pairs
1567D - Expression Evaluation Error
78A - Haiku
1287A - Angry Students
1428A - Box is Pull
234B - Reading
581B - Luxurious Houses
1481C - Fence Painting
935A - Fafa and his Company
22A - Second Order Statistics
1720B - Interesting Sum
1720A - Burenka Plays with Fractions
3A - Shortest path of the king
1720C - Corners
574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils